package com.gitee.hljdrl.dialog;


import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.gitee.hljdrl.dialog.basis.ViewPartVisitor;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;


public class UISheetDialog extends BottomSheetDialog {

    protected String TAG = "UISheetDialog";

    protected boolean mFixAnim = true;

    private int mWindowAnimations = 0;

    protected View mView;

    private ViewPartVisitor mViewPartVisitor;

    public UISheetDialog(@NonNull Context context) {
        this(context, R.style.UIBottomSheetDialog);
    }

    public UISheetDialog(@NonNull Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mViewPartVisitor != null) {
            View contentView = View.inflate(getContext(), mViewPartVisitor.getLayout(), null);
            mView = contentView;
            setContentView(contentView);
            initView(mView);
        } else {
            Log.i(TAG, "layoutId=0 Error. ");
        }
        BottomSheetBehavior<FrameLayout> behavior = getBehavior();
        behavior.setDraggable(false);
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }


    protected void initView(View contentView) {
        if (mViewPartVisitor != null) {
            try {
                mViewPartVisitor.onViewVisit(this, contentView);
            } catch (Exception ex) {
                //TODO
                ex.printStackTrace();
                Log.e(TAG, "initView onViewVisit Error ", ex);
            }
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Log.i(TAG, "onWindowFocusChanged-->" + hasFocus);
        if (mFixAnim) {
            if (hasFocus) {
                if (mWindowAnimations != 0) {
                    getWindow().getDecorView().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            getWindow().setWindowAnimations(mWindowAnimations);
                        }
                    }, 300);
                }
            } else {
                mWindowAnimations = getWindow().getAttributes().windowAnimations;
                getWindow().setWindowAnimations(0);
            }
        }
    }

    public UISheetDialog setViewPartVisitor(ViewPartVisitor partVisitor) {
        this.mViewPartVisitor = partVisitor;
        return this;
    }

    @Override
    public void dismiss() {
        Log.i(TAG, "dismiss");
        if (mViewPartVisitor != null) {
            if (mViewPartVisitor.onDismiss(this)) {
                return;
            }
        }
        super.dismiss();
    }

    @Override
    public void show() {
        Log.i(TAG, "show");
        super.show();
        if (mViewPartVisitor != null) {
            mViewPartVisitor.onShow();
        }
    }

}
